﻿// =====================================================
// Copyright (C) 2017-2018 Fanjia
// All rights reserved
// CLR版 本: 4.0.30319.42000
// 创建时间: 2018/1/22 10:31:30
// 创 建 人: 李伟
// ======================================================

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication1
{
    public class Tools
    {
        /// <summary>
        /// 写入文件
        /// </summary>
        /// <param name="logInfo">内容</param>
        /// <param name="fileName"></param>
        public static void WriteInfoToFileNoRw(string logInfo, string fileName = "")
        {
            string sMonth = DateTime.Now.ToString("MM");
            string sYear = DateTime.Now.ToString("yyyy");
            string sDay = DateTime.Now.ToString("dd");

            string sFileName = string.Empty;

            if (!string.IsNullOrEmpty(fileName))
            {
                sFileName = AppDomain.CurrentDomain.BaseDirectory + "log/" + fileName + ".log";
            }
            else
            {
                sFileName = AppDomain.CurrentDomain.BaseDirectory + "log/" + sYear + "/" + sMonth + "/" + sDay + ".log";
                //DateTime.Now.ToString("yyyy-MM-ddHHmmssfff") + ".log";
            }

            if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "log/"))
            {
                Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "log/");
            }
            if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "log/" + sYear))
            {
                Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "log/" + sYear);
            }
            if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "log/" + sYear + "/" + sMonth))
            {
                Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "log/" + sYear + "/" + sMonth);
            }
            if (!File.Exists(sFileName))
            {
                File.Create(sFileName).Close();
            }

            //写入文件
            System.IO.StreamWriter sw = new System.IO.StreamWriter(sFileName, true);
            sw.WriteLine(logInfo);
            sw.Close();
        }

        /// <summary>
        /// 写入文件
        /// </summary>
        /// <param name="logInfo">内容</param>
        /// <param name="fileName"></param>
        public static void WriteInfoToFile(string logInfo, string fileName = "")
        {
            string sMonth = DateTime.Now.ToString("MM");
            string sYear = DateTime.Now.ToString("yyyy");
            string sDay = DateTime.Now.ToString("dd");

            string sFileName = string.Empty;

            if (!string.IsNullOrEmpty(fileName))
            {
                sFileName = AppDomain.CurrentDomain.BaseDirectory + "log/" + fileName + ".log";
            }
            else
            {
                sFileName = AppDomain.CurrentDomain.BaseDirectory + "log/" + sYear + "/" + sMonth + "/" + sDay + ".log";
                //DateTime.Now.ToString("yyyy-MM-ddHHmmssfff") + ".log";
            }

            if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "log/"))
            {
                Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "log/");
            }
            if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "log/" + sYear))
            {
                Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "log/" + sYear);
            }
            if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "log/" + sYear + "/" + sMonth))
            {
                Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "log/" + sYear + "/" + sMonth);
            }
            if (!File.Exists(sFileName))
            {
                File.Create(sFileName).Close();
            }

            //写入文件
            System.IO.StreamWriter sw = new System.IO.StreamWriter(sFileName, false);
            sw.WriteLine(logInfo);
            sw.Close();
        }
    }
}
